package com.global.college.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.roundview.RoundTextView;
import com.global.college.R;

/**
 * @author: Lgg
 * @datetime: 2021/7/1
 */
public class HsSendVerifyCodeButtonNoBg extends LinearLayout {
    private Runnable mRunnable = new MyRunnable(this);
    private RoundTextView tvAccessVerify;
    private TextView tvScending;
    private CountDownTimer mCountDownTimer;
    private OnVerifyClickListener mVerifyClickListener;
    private Context mContext;

    /**
     * 对外提供接口,处理访问网络数据的操作
     */
    public interface OnVerifyClickListener {
        void onVerify();
    }

    public HsSendVerifyCodeButtonNoBg(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mContext = context;
        View.inflate(context, R.layout.button_verify_no_bg, this);
        this.tvAccessVerify = findViewById(R.id.verify_true);
        this.tvScending = findViewById(R.id.verify_false);
        this.tvAccessVerify.setOnClickListener(v -> {
            if (mVerifyClickListener != null) {
                mVerifyClickListener.onVerify();
            }
        });
    }


    public void setOnVerifyClickListener(OnVerifyClickListener verifyClickListener) {
        this.mVerifyClickListener = verifyClickListener;
    }

    public boolean getIsStart() {
        return VISIBLE == this.tvScending.getVisibility() ? true : false;
    }

    /**
     * 验证码发送结束，按钮变成有颜色
     */
    private void setVerifyVisible() {
        this.tvScending.setVisibility(View.GONE);
        this.tvAccessVerify.setVisibility(View.VISIBLE);
        stopCountDownTimer();
    }

    /**
     * 显示正在发送(*s)
     */
    private void setVerifyUnVisible() {
        this.tvScending.setVisibility(View.VISIBLE);
        this.tvAccessVerify.setVisibility(View.GONE);
    }

    /**
     * 开启计时
     */
    public void startCountDownTimer() {
        setVerifyUnVisible();
        new Handler().postDelayed(this.mRunnable, 0);
    }

    /**
     * 停止计时
     */
    private void stopCountDownTimer() {
        if (this.mCountDownTimer != null) {
            this.mCountDownTimer.cancel();
        }
    }

    /**
     * 开启子线程
     */
    class MyRunnable implements Runnable {
        final HsSendVerifyCodeButtonNoBg sendVerifyCodeButton;

        MyRunnable(HsSendVerifyCodeButtonNoBg sendVerifyCodeButton) {
            this.sendVerifyCodeButton = sendVerifyCodeButton;
        }

        @Override
        public void run() {
            //从60s开始以秒为单位倒计时
            this.sendVerifyCodeButton.mCountDownTimer = new MyCountDownTimer(this, 60000L, 1000).start();
        }

    }

    /**
     * 设定一个计时器
     */
    class MyCountDownTimer extends CountDownTimer {
        final MyRunnable mRunnable;

        public MyCountDownTimer(MyRunnable runnable, long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//millisInFuture：倒计时的总时间，countDownInterval:计时的时间间隔

            this.mRunnable = runnable;
        }

        @Override
        public void onTick(long millisUntilFinished) {
//            this.mRunnable.sendVerifyCodeButton.tvScending.setText("已发送 (" + ((int) (millisUntilFinished / 1000)) + "s)");
            long showTime = millisUntilFinished / 1000;
            mContext.getString(R.string.get_vercode_tip, showTime + "");
            this.mRunnable.sendVerifyCodeButton.tvScending.setText(mContext.getString(R.string.get_vercode_tip, showTime + ""));
        }

        @Override
        public void onFinish() {
            this.mRunnable.sendVerifyCodeButton.tvScending.setText(R.string.get_vercode_again);
            this.mRunnable.sendVerifyCodeButton.mCountDownTimer = null;
            this.mRunnable.sendVerifyCodeButton.setVerifyVisible();
        }

    }
}
